<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/9/18
 * Time: 18:12
 */
namespace App\Http\Controllers\Home;
use Illuminate\Support\Facades\Redis;
class RedisLockController {
    private $id;

    public function __construct($id)
    {
        $this->id= $id;
    }

    public function lock() {
        return Redis::set("orders:lock", $this->id, "nx", "ex", 10);
    }

    function unlock() {
        $script = <<<LUA
if redis.call("get",KEYS[1]) == ARGV[1]
then
    return redis.call("del",KEYS[1])
else
    return 0
end
LUA;
        return Redis::eval($script, 1, "orders:lock", intval($this->id));
    }
}